package com.sunday.volunteer.ui.product;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.adapter.ConfirmOrderAdapter;
import com.sunday.volunteer.entity.Address;
import com.sunday.volunteer.entity.Cart;
import com.sunday.volunteer.entity.Order;
import com.sunday.volunteer.entity.Product;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.ui.mine.OrderActivity;
import com.sunday.volunteer.ui.user.AddressActivity;
import com.sunday.volunteer.utils.BaseUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Pual's PC on 2016/10/12.
 */
public class ConfirmOrderActivity extends BaseActivity {
    @Bind(R.id.title_text)
    TextView title_text;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.amout)
    TextView amout;
    private ConfirmOrderAdapter adapter;
    private Cart cart;
    private SparseArray sparseArray;
    private Long addressId = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_order);
        ButterKnife.bind(this);
        title_text.setText("确认订单");

        sparseArray = new SparseArray();
        cart = (Cart) getIntent().getSerializableExtra(BaseUtil.ITEM);
        if (cart == null) {
            return;
        }
        sparseArray.put(0, cart);
        adapter = new ConfirmOrderAdapter(sparseArray, new int[]{R.layout.item_address, R.layout.item_order_product, R.layout.layout_confirm_order});
        adapter.setOnClickListener(onClickListener);
        //adapter.setType(getIntent().getExtras().getInt(BaseUtil.TYPE, -1));
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);

        BaseUtil.setText(amout , cart.getPayAmount());
        //getAddress();

    }

    @OnClick(R.id.next_btn)
    void nextBtn() {
        if (addressId == null) {
            ToastUtils.showToast(mContext, "请选择收货地址!");
            return;
        }
        showLoadingDialog(R.string.save);
        createOrder();
    }


    private void createOrder() {
        Call<ResultDO<Order>> call = HttpClient.getHttpAdapter().createOrder(BaseApp.getInstance().getMemberId() , getIds(), addressId);
        call.enqueue(new Callback<ResultDO<Order>>() {
            @Override
            public void onResponse(Call<ResultDO<Order>> call, Response<ResultDO<Order>> response) {
                dissMissDialog();
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    ToastUtils.showToast(mContext, "兑换成功");
                    BaseUtil.openActivity(mContext , OrderActivity.class);
                     finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Order>> call, Throwable t) {

            }
        });
    }

    private String getIds() {
        String ids = "";
        List<Product> cartList = cart.getProducts();
        for (Product item : cartList) {
            if (TextUtils.isEmpty(ids)) {
                ids = ids + item.getCartId();
            } else {
                ids = ids + " ," + item.getId();
            }
        }
        return ids;
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.to_address) {
                intent = new Intent(mContext, AddressActivity.class);
                intent.putExtra(BaseUtil.ITEM, "aaa");
                startActivityForResult(intent, 0000);
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0000 && resultCode == RESULT_OK) {
            Address address = (Address) data.getSerializableExtra(BaseUtil.ITEM);
            addressId = address.getId();
            sparseArray.put(2, address);
            adapter.notifyDataSetChanged();
        }
    }

}
